using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Common;
using Net.SourceForge.WURFL.Core;
namespace Roaming.view
{
    public partial class info : System.Web.UI.Page
    {
        protected int iCateId = 0;
        protected int cols = 3;
        protected DataSet ds = null;
        protected DataSet dsMenu = null;
        protected DataSet dsArticle = null;
        private int PageIndex = 0;
        protected int curPage = 0;
        protected string strPageLink = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string strUA = Request.Headers["USER-AGENT"].ToString();
            if (strUA.ToLower().Contains("opera"))
            {
                if (Request.Headers["X-OperaMini-Phone-UA"] != null)
                    strUA = Request.Headers["X-OperaMini-Phone-UA"].ToString();
            }
            cols = DAL.getColLinkSite(strUA);
            if (cols > 2)
            {
                cols = 3;
            }
            if ((Request.QueryString["catId"] != null) && (Request.QueryString["catId"].ToString() != ""))
                iCateId = Int32.Parse(Request.QueryString["catId"].ToString());
            curPage = PageIndex = ((Request.QueryString["page"]) == null) ? 0 : Convert.ToInt32(Request.QueryString["page"].ToString());
            if (!Page.IsPostBack)
            {
                if (iCateId == 342)
                {
                    string iCity = Session["CityId"].ToString();
                    Response.Redirect("/"+iCity+"/travel Ag.html");
                }
                if (iCateId > 0)
                    BindDataById(iCateId);
            }
            if (PageIndex!=0)
            Session["Infopage"] = PageIndex.ToString(); 
            else
            Session["Infopage"] = "1"; 
        }

        protected void BindDataById(int iCateId)
        {
     

            string strSQL1 = "";
            strSQL1 = string.Format("exec tblCategory_GetById {0}", iCateId);
            DataSet dsChild = null;
            dsChild = DAL.GetDataSet(strSQL1);

            string strSQL2 = "";
            if (Int32.Parse(dsChild.Tables[0].Rows[0]["parentId"].ToString()) > 0)
            {
                strSQL2 = string.Format("exec tblCategory_GetById {0}", Int32.Parse(dsChild.Tables[0].Rows[0]["parentId"].ToString()));
            }
            else
            {
                strSQL2 = string.Format("exec tblCategory_GetById {0}", Int32.Parse(dsChild.Tables[0].Rows[0]["categoryId"].ToString()));
            }
            dsMenu = DAL.GetDataSet(strSQL2);

            string strSQL3 = "";
            strSQL3 = string.Format("exec tblCategory_GetAllByCondition N'{0}'", "AND status=1 AND parentId=" + iCateId.ToString());
            DataSet dsCheck = DAL.GetDataSet(strSQL3);
            if (dsCheck.Tables[0].Rows.Count > 0) //Category
            {
                if (PageIndex == 0) PageIndex = 1;
                int count = 0;
                ArrayList arr = null;
                try
                {
                    arr = DAL.GetDataReaderToArrayList(strSQL3);
                    count = arr.Count;                    
                    int iRecordPerPage = 0;
                    if (iCateId == 92)
                    {
                        if (cols == 2)
                        {
                            iRecordPerPage = 8;
                        }
                        else
                            iRecordPerPage = 9;
                    }
                    else
                        iRecordPerPage = 5;
                    int PageCount = (int)count % iRecordPerPage;
                    if (PageCount == 0)
                        PageCount = (int)count / iRecordPerPage;
                    else
                        PageCount = (int)count / iRecordPerPage + 1;
                    if (PageIndex > PageCount) PageIndex = PageCount;
                    if ((PageCount == 0) || (PageCount == 1))
                    {
                        strPageLink = "<a href=\"/" + "view/index.html" + ".htm\">" + "Back" + "</a> | ";
                        PageIndex = 1;
                    }
                    else
                    {
                        strPageLink = WritePageLink(PageIndex, PageCount);
                    }
                    ds = DAL.ListImageData(strSQL3, PageIndex, iRecordPerPage, "ItemImageMms");
                    rptList.DataSource = ds;
                    rptList.DataBind();
                    rptList.Dispose();
                }
                catch (Exception e)
                {
                    e.ToString();
                    ds.Dispose();
                }
            }
            else // Display new article + other article of CateId
            {
                string strSQL4 = "";
                strSQL4 = string.Format("exec tblArticle_GetTopNAndCondition {0},N'{1}'", 1, "AND status=1 AND categoryId=" + iCateId.ToString());
                dsArticle = DAL.GetDataSet(strSQL4);
                if (dsArticle.Tables[0].Rows.Count > 0)
                    Response.Redirect("/" + dsArticle.Tables[0].Rows[0]["articleId"].ToString()+"/infoDetails.html");
            }
        }
        protected string getTopByCate(string catId)
        {
            string strSQL = string.Format("exec tblArticle_GetTopNByCateId {0},{1}", 1, catId);
            DataSet ds = Common.DAL.GetDataSet(strSQL);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["message"].ToString();
               
            }
            else
                return "";            
        }
        protected string getTopByCate_img(string catId)
        {
            string strSQL = string.Format("exec tblArticle_GetTopNByCateId {0},{1}", 1, catId);
            string tmp = "";
            DataSet ds = Common.DAL.GetDataSet(strSQL);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                tmp = ds.Tables[0].Rows[0]["source"].ToString();
                ds.Dispose();
            }
            else
                tmp= "";
            strSQL = string.Format("exec tblWeatherIcon_GetByKey '{0}'", tmp);
            ds = Common.DAL.GetDataSet(strSQL);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["Img"].ToString();
                
            }
            else
                return "";
                       

        }
        protected string getTopByCate_Title(string catId)
        {
            string strSQL = string.Format("exec tblArticle_GetTopNByCateId {0},{1}", 1, catId);
            DataSet ds = Common.DAL.GetDataSet(strSQL);
            if (ds != null && ds.Tables[0].Rows.Count > 0)
            {
                return ds.Tables[0].Rows[0]["title"].ToString();
                
            }
            else
                return "";
            
        }
        private string WritePageLink(int CurrentPage, int PageCount)
        {

            string sHTML = "";

            if (PageCount <= 3)
            {

                for (int i = 1; i <= PageCount; i++)
                {
                    if (i > 1)
                        sHTML += " | ";
                    sHTML += BuildURL(Convert.ToSingle(i) + "  ", i);
                }
                return sHTML;
            }
            else
            {
                if (CurrentPage <= 3)
                {
                    for (int i = 1; i <= 3; i++)
                    {
                        if (i > 1)
                            sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(i) + "  ", i);
                    }
                    sHTML += BuildURL("Next |", CurrentPage + 1);
                    return sHTML;
                }
                else
                {
                    if (CurrentPage == PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 2) + "  ", CurrentPage - 2);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += BuildURL(" Back |", CurrentPage - 1);
                        return sHTML;
                    }
                    if (CurrentPage + 1 == PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage + 1) + "  ", CurrentPage + 1);
                        sHTML += BuildURL(" Back |", CurrentPage - 2);
                        return sHTML;
                    }
                    if (CurrentPage + 1 < PageCount)
                    {
                        //sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage - 1) + "  ", CurrentPage - 1);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage) + "  ", CurrentPage);
                        sHTML += " | ";
                        sHTML += BuildURL(Convert.ToSingle(CurrentPage + 1) + "  ", CurrentPage + 1);
                        sHTML += BuildURL(" Next |", CurrentPage + 1);
                        return sHTML;
                    }

                }
            }
            return sHTML;
        }
        private string BuildURL(string text, int PageIndex)
        {
            string result = "";
            if (curPage != PageIndex)
            {
                //result = "<a href=\"info.aspx?catId=" + iCateId + "&page=" + PageIndex + "\">" + text + "</a>";
                result = "<a href=\"/" + iCateId + "/info-page" + PageIndex + ".html\">" + text + "</a>";
            }
            else
                result = text;
            return result;
        }
    }
}
